const {Service}=require("egg")

const axios =require("axios")

class UserService extends Service{

    // oss;
    async getOss({id,imgurl}){
        // const sql=`select * from login avatar=? where id=?`
        const sql=`update login set avatar=? where id=?`
        return this.app.mysql.query(sql,[imgurl,id])
    }

    // 获取用户信息
    async getAccess(access_token){
        const result=axios.get("https://gitee.com/api/v5/user",{params:{access_token}})
        return result
    }

    // 第三方登录
    async getCodes(code){
        const result =axios.post("https://gitee.com/oauth/token",{
            grant_type:"authorization_code",
            code,
            client_id:"dbcf2d848ff9ad0cd039c8bc5dd6048a8e66f52f149366e8b44e7689cc05cf6d",
            redirect_uri:"http://localhost:8080/home/index",
            client_secret:"84aa27c2fc4a39295d1f483418ad895de0427c68110af1ddcf1ddcac3adb657e"
        })
        return result
    }

    // 模糊搜索
    async filter(text){
        const sql=`select * from user_list where username like "%${text}%"`
        return await this.app.mysql.query(sql)
    }

    // 删除;
    async destroy(id){
        const sql=`delete from user_list where id=?`
        return await this.app.mysql.query(sql,[id])
    }

    // 查看
    async show(id){
        const sql=`select * from user_list where id=?`
        return await this.app.mysql.query(sql,[id])
    }

    // 编辑
    async update(id,username,nickname,age){
        const sql=`update user_list set username=?,nickname=?,age=? where id=?`
        return await this.app.mysql.query(sql,[username,nickname,age,id])
    }

    // 增加
    async create(id,username,nickname,age){
        const sql=`insert into user_list (id,username,nickname,age) values (?,?,?,?)`
        return await this.app.mysql.query(sql,[id,username,nickname,age])
    }

    // 获取数据;
    async index(){
        const sql=`select * from user_list`
        return await this.app.mysql.query(sql)
    }

    // 判断;
    async chengName(username){
        const sql=`select * from login where username=?`
        return await this.app.mysql.query(sql,[username])
    }

    // 注册;
    async registry(id,username,pwd){
        const sql=`insert into login (id,username,pwd) values (?,?,?)`
        return await this.app.mysql.query(sql,[id,username,pwd])
    }

    // 登录
    async login(username,pwd){
        const sql=`select * from login where username=? and pwd=?`
        return await this.app.mysql.query(sql,[username,pwd])
    }
}

module.exports=UserService